/**
 * Filename: AlbumService.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/2/29 18:44
 */
package com.github.postsanf.yinian.service;

import android.content.Intent;
import android.util.Log;

import com.github.postsanf.yinian.BaseIntentService;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNBanner;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNAlbumResponse;
import com.github.postsanf.yinian.entity.YNBannerResponse;
import com.github.postsanf.yinian.entity.YNCommonResponse;
import com.github.postsanf.yinian.entity.YNLoginResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 相册后台服务
 */
public class AlbumService extends BaseIntentService {
    public AlbumService() {
        super("AlbumService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            switch (action) {
                // 根据UID获取用户相册信息
                case ACTIONs.actionGetFristAlbum:
                    getFristAlbumListById(intent.getExtras().getInt(ACTIONs.aUid));
                    break;
                case ACTIONs.actionGetAlbum:
//                    getIsNewUser();
                    getAlbumListById(intent.getExtras().getInt(ACTIONs.aUid));
                    break;
                case ACTIONs.actionGetUser:
                    getUser();
                    break;
                case ACTIONs.actionBanner:
                    getBannerInfo();
                    break;
                case ACTIONs.activityBinner:
                    getactivityBinner();
                    break;
            }
        }
    }

    private void getIsNewUser() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynJudgeUserIsNew))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNCommonResponse isNewUser = gson.fromJson(s, YNCommonResponse.class);
                        // 通知界面显示view
                        Intent upIntent = new Intent(ACTIONs.actionIsNewUser);
                        boolean isnew;
                        if (isNewUser != null && ReqUtils.isNewUser(isNewUser.getCode())) {
                            isnew = false;
                        } else {
                            isnew = true;
                        }
                        upIntent.putExtra(ACTIONs.aAddData, isnew);
                        sendBroadcast(upIntent);
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    private void getBannerInfo() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynGetMainBanner))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNBannerResponse resBanner = gson.fromJson(s, YNBannerResponse.class);
                        if (resBanner != null && ReqUtils.isSuccess(resBanner.getCode())) {
                            List<YNBanner> bannerList = new ArrayList<YNBanner>();
                            bannerList.addAll(resBanner.getData());
                            // 通知Banner更新
                            Intent upIntent = new Intent(ACTIONs.actionBannerUpdate);
                            upIntent.putExtra(ACTIONs.aAddData, (Serializable) bannerList);
                            sendBroadcast(upIntent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    private void getUser() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynShowPerInfo))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNLoginResponse resUser = gson.fromJson(s, YNLoginResponse.class);
                        if (resUser != null && ReqUtils.isSuccess(resUser.getCode()) && resUser.getData().size() > 0) {
                            application.setCurUser(resUser.getData().get(0));
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 获取用户相册信息
    private void getAlbumListById(int uId) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(uId));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynShowPhoto))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
//                        Log.i("xjs", "getAlbumListById===>" + s);
                        YNAlbumResponse resGroup = gson.fromJson(s, YNAlbumResponse.class);
                        if (resGroup != null && ReqUtils.isSuccess(resGroup.getCode())) {
                            YNSingleton.getInstance().clearAlbumList();
                            YNSingleton.getInstance().getAlbumList().addAll(resGroup.getData());
                            sendBroadcast(new Intent(ACTIONs.actionUpdateAlbum));
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 获取用户相册信息
    private void getFristAlbumListById(int uId) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(uId));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynShowPhoto))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNAlbumResponse resGroup = gson.fromJson(s, YNAlbumResponse.class);
                        if (resGroup != null && ReqUtils.isSuccess(resGroup.getCode())) {
                            YNSingleton.getInstance().clearAlbumList();
                            YNSingleton.getInstance().getAlbumList().addAll(resGroup.getData());
                            // 通知列表更新
                            if (resGroup.getData() != null && resGroup.getData().size() > 0) {
                                YNSingleton.getInstance().setCurAlbum(YNSingleton.getInstance().getAlbumList().get(0));
                                application.setCurGid(YNSingleton.getInstance().getAlbumList().get(0).getGroupid());
                                sendBroadcast(new Intent(ACTIONs.actionUpdateFristAlbum).putExtra("update", true));
                            } else {
                                sendBroadcast(new Intent(ACTIONs.actionUpdateEmptyAlbum));
                            }
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                        sendBroadcast(new Intent(ACTIONs.actionUpdateEmptyAlbum));
                    }
                });
        liteHttp.executeAsync(req);
    }


    // 获取用户相册信息
    private void getactivityBinner() {
        StringRequest req = new StringRequest("https://api.zhuiyinanian.com/YinianProject/yinian/GetActivityList")
                .setMethod(HttpMethods.Post)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNBannerResponse resBanner = gson.fromJson(s, YNBannerResponse.class);
                        if (resBanner != null && ReqUtils.isSuccess(resBanner.getCode())) {
                            List<YNBanner> bannerList = new ArrayList<YNBanner>();
                            bannerList.addAll(resBanner.getData());
                            // 通知Banner更新
                            Intent upIntent = new Intent(ACTIONs.actitityBannerUpdate);
                            upIntent.putExtra(ACTIONs.aAddData, (Serializable) bannerList);
                            sendBroadcast(upIntent);
//                            Log.d("yuhao", "binner="+bannerList.get(0).getBtype());
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
